﻿using Puddy.Models;
using Puddy.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;

namespace Puddy.Utils
{
    public class RestoreData
    {
        //save location list
        public static async void CreateJsonLocationList(ObservableCollection<LocationViewModel> locationList)
        {
            try {
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(locationList.GetType());

                StorageFile outputFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("LocationList", CreationCollisionOption.ReplaceExisting);

                Stream stream = await outputFile.OpenStreamForWriteAsync();

                jsonSerializer.WriteObject(stream, locationList);

                stream.Close();
            }
            catch(Exception e)
            {
                Debug.WriteLine("error save file: " + e.Message);
            }
            

        }

        //get location list
        public static async Task<Object> GetLocationList()
        {
            try
            {
                
                StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("LocationList");
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ObservableCollection<LocationViewModel>));
                Stream stream = await file.OpenStreamForReadAsync();

                var locationList = jsonSerializer.ReadObject(stream);

                stream.Close();
                return locationList;
            }
            catch
            {
                // file not exist
                return null;
            }
        }


        // save setting alarm
        public static async Task CreateJsonSetting(SettingAlarm settingAlarm)
        {
            try
            {
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(settingAlarm.GetType());

                StorageFile outputFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("SettingAlarm", CreationCollisionOption.ReplaceExisting);

                Stream stream = await outputFile.OpenStreamForWriteAsync();

                jsonSerializer.WriteObject(stream, settingAlarm);

                stream.Close();
            }
            catch
            {

            }
            

        }

        //get setting alarm
        public static async Task<SettingAlarm> GetSettingAlarm()
        {
            try
            {
                StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("SettingAlarm");

                
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(SettingAlarm));
                Stream stream = await file.OpenStreamForReadAsync();

                SettingAlarm setting = jsonSerializer.ReadObject(stream) as SettingAlarm;

                stream.Close();

                return setting;
            }
            catch
            {
                //file not exist
                return null;
            }
        }

        
    }
}
